//
//  WorldBaseViewController.m
//  markmap
//
//  Created by 甘世清 on 2024/4/24.
//

#import "WorldBaseViewController.h"
#import "WorldViewController.h"
#import "HomeTopModel.h"
#import "MyMapViewController.h"
#import "ListMiViewController.h"
#import "HomeSetupPopView.h"
@interface WorldBaseViewController ()
@property (nonatomic , strong) SQCustomButton * rightBtn;
@end

@implementation WorldBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self SQ_uiViewForSelf];
    [self SQ_showPinglun];
}
-(void)SQ_uiViewForSelf{
    self.tabBar.indicatorColor = SQColor(@"#E133FF");
    self.tabBar.indicatorCornerRadius = 2;
    self.tabBar.leadingSpace = 10;
    self.tabBar.trailingSpace = 10;
    self.tabContentView.delegate = self;
    self.tabContentView.loadViewOfChildContollerWhileAppear = YES;
    self.tabBar.itemTitleSelectedFont = FONT_MEDIUM(18);
    self.tabBar.itemTitleFont = FONT_REGULAR(16);
    self.tabBar.itemTitleColor = SQColor(@"#AAAAAA");
    self.tabBar.itemTitleSelectedColor = UIColor.whiteColor;
    self.tabBar.backgroundColor = UIColor.clearColor;
    [self.tabBar setItemContentHorizontalCenterAndMarginTop:10 spacing:0];
    [self.tabBar setIndicatorInsets:UIEdgeInsetsMake(34, 10, 8, 10) tapSwitchAnimated:YES];
    [self.tabBar setScrollEnabledAndItemFitTextWidthWithSpacing:30 minWidth:70];
    [self.tabContentView setContentScrollEnabled:YES tapSwitchAnimated:YES];
     self.tabContentView.backgroundColor = SQColor333333;
    [self setTabBarFrame:CGRectMake(0, TopHeight-44, SCREEN_WIDETH, 44)
        contentViewFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
  
    UserModel * model = [UserModel share];
    if ([model.userid isEqualToString:@"125"]) {
        [self SQ_header125];
    } else {
        [self SQ_header];
    }
    [self.view addSubview:self.rightBtn];
    [self.rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(60);
            make.height.mas_equalTo(30);
        make.centerY.equalTo(self.tabBar);
        make.right.equalTo(self.view.mas_right);
    }];
  
    [self.rightBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [HomeSetupPopView SQ_Show];
    }];
}
-(void)SQ_header125{
    [self setTabBarFrame:CGRectMake(0, TopHeight-44, SCREEN_WIDETH-60, 44)
        contentViewFrame:CGRectMake(0, 0, SCREEN_WIDETH, SCREEN_HEIGHT)];
    self.rightBtn.hidden = NO;
    HomeTopModel * model0 = [HomeTopModel new];
    model0.type = 0;
    model0.name = @"附近觅点";
    HomeTopModel * model1 = [HomeTopModel new];
    model1.type = 1;
    model1.name = @"附近觅友";
    HomeTopModel * model2 = [HomeTopModel new];
    model2.type = 2;
    model2.name = @"最新觅点";
    HomeTopModel * model3 = [HomeTopModel new];
    model3.type = 3;
    model3.name = @"最新";
    HomeTopModel * model4 = [HomeTopModel new];
    model4.type = 4;
    model4.name = @"在线";
    NSArray * array = @[model0,model1,model2,model3,model4];
    [self SQ_TopList:array];
}
-(void)SQ_header{
    
    HomeTopModel * model0 = [HomeTopModel new];
    model0.type = 6;
    model0.name = @"新增";
    HomeTopModel * model1 = [HomeTopModel new];
    model1.type = 7;
    model1.name = @"附近";
    HomeTopModel * model2 = [HomeTopModel new];
    model2.type = 8;
    model2.name = @"在线";
    HomeTopModel * model3 = [HomeTopModel new];
    model3.type = 9;
    model3.name = @"已认证";
    NSArray * array = @[model0,model1,model2,model3];
    [self SQ_TopList:array];
}
-(void)SQ_TopList:(NSArray*)array{
    NSMutableArray * top = [[NSMutableArray alloc]init];
    for (HomeTopModel * model in array) {
        if (model.type > 4) {
            ListMiViewController * vc = [[ListMiViewController alloc]init];
            vc.yp_tabItemTitle =  model.name;
            vc.topModel = model;
            [top addObject:vc];
        } else {
            WorldViewController * vc = [[WorldViewController alloc]init];
            vc.yp_tabItemTitle =  model.name;
            vc.topModel = model;
            [top addObject:vc];
            _rightBtn.hidden = NO;
        }
    }
    self.viewControllers = [[NSMutableArray alloc]initWithArray:top];
}
 
/**
 *  是否能切换到指定index
 */
- (BOOL)tabContentView:(YPTabContentView *)tabConentView shouldSelectTabAtIndex:(NSUInteger)index{
    if (self.rightBtn.hidden) {
        return YES;
    }
    if ([UserModel share].isShow) {
        return YES;
    } else {
        if (index == 0) {
            return YES;
        }
        [self SQ_headerButtonClick:index];
        return NO;
    }
}
-(void)SQ_headerButtonClick:(NSInteger)index{
   
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:SQNSLocalString(@"是否允许展示您的当前位置") message:@"使用该功能时,可授权允许附近用户查看到您的当前位置,您随时可于右上角设置中进行隐藏当前位置" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction * co0 = [UIAlertAction actionWithTitle:SQNSLocalString(@"确定") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [YJProgressHUD SQ_showMsgWithoutView:@""];
       NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setObject:@"1" forKey:@"isShow"];
        [HttpTool post:Url5 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
            UserModel * model =[UserModel share];
            model.isShow = YES;
            [UserModel updateLocalUserModel:model];
            self.tabContentView.selectedTabIndex = index;
            } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
                [YJProgressHUD showMsgWithoutView:httpModel.message];
            }];
    }];
 
    UIAlertAction * co2 = [UIAlertAction actionWithTitle:SQNSLocalString(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
       
    }];
    [alert addAction:co0];
    [alert addAction:co2];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)SQ_showPinglun{
    NSString * pinglunShow = [Manager SQ_stringForKey:@"pinglunShow"];
    if ([pinglunShow isNotEmpty]) {
        if ([pinglunShow intValue] == 5) {
            if ([SKStoreReviewController respondsToSelector:@selector(requestReviewInScene:)]) {
                NSArray *array =[[[UIApplication sharedApplication] connectedScenes] allObjects];
                UIWindowScene *windowScene = (UIWindowScene *)array[0];
                [SKStoreReviewController requestReviewInScene:windowScene];
            }
        }
        [Manager SQ_SetObject:[NSString stringWithFormat:@"%d",[pinglunShow intValue]+1] FormKey:@"pinglunShow"];
        if ([pinglunShow intValue] > 20) {
            [Manager SQ_SetObject:@"1" FormKey:@"pinglunShow"];
        }
    } else {
        [Manager SQ_SetObject:@"1" FormKey:@"pinglunShow"];
    }
}

- (SQCustomButton *)rightBtn{
    if (!_rightBtn) {
        _rightBtn = [[SQCustomButton alloc]initType:SQCustomButtonTopImageType imageSize:CGSizeMake(24, 18) midmargin:0];
        _rightBtn.image = SQGetImage(@"shezhi 1");
        _rightBtn.hidden = YES;
    }
    return _rightBtn;
}
@end
